import {Cell} from "./cell";

class Fence {
    cells = []
    specs
    title
    id

    constructor(specs) {
        this.specs = specs
        this.title = specs[0].key  //规格名
        this.id = specs[0].key_id   //规格名id
    }

    init() {
        this._initCells()
    }

    _initCells() {
        // some 要求数组任意一个元素满足就返回true
        // every 要求数组所有元素都满足条件才返回true，不然返回false
        this.specs.forEach(s => {
            const existed = this.cells.some(c => {
                return c.id === s.value_id
            })
            if (existed) {
                return
            }
            const cell = new Cell(s);
            this.cells.push(cell)
        })
    }

    setFenceSketch(skuList) {
        this.cells.forEach(cell => {
            this._setCellSkuImg(cell, skuList)
        })
    }

    _setCellSkuImg(cell, skuList) {
        const specCode = cell.getCellCode()
        const matchedSku = skuList.find(s => s.code.includes(specCode)) // 找到第一个就返回
        if (matchedSku) {
            cell.setSkuImg(matchedSku.img)
        }
    }

}


export {
    Fence
}